package com.cskaoyan.exception;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.beanwx.vo.WxBaseRespVo;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.mail.MailSendException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.naming.AuthenticationException;

/**
 * @author: lds
 * @clsss: ExceptionControllerAdvice
 */
@RestControllerAdvice
//@ResponseBody
//@RestControllerAdvice = @ControllerAdvice + @ResponseBody
public class ExceptionControllerAdvice {
    //和handler方法的写法基本是一致的
    @ExceptionHandler(ArithmeticException.class)
    public BaseRespVo arithmeticException(ArithmeticException e) {
        String message = e.getMessage();
        return BaseRespVo.fail("发生算数异常" + message);
    }

    /**
     * @author: lds
     * @scope: global(全局)
     */
    @ExceptionHandler(AuthenticationException.class)
    public BaseRespVo handleAuthenticationException(Exception e){
        return BaseRespVo.fail("没有权限");
    }

    /**
     * @author: lds
     * @scope: global(全局)
     * @description: 可以通过一个方法处理多种类型的异常
     */
    @ExceptionHandler({ParameterException.class})
    @ResponseBody
    public BaseRespVo parameterException(Exception exception) {
        exception.printStackTrace();
        return BaseRespVo.fail("参数有误");
    }
    /**
     * @author: lds
     * @scope: global(全局)
     */
    @ExceptionHandler({DataDisplayException.class})
    @ResponseBody
    public BaseRespVo dataDisplayException() {
        return BaseRespVo.fail("数据显示异常");
    }

    /**
     * @author: lds
     * @scope: local(局部)
     */
    @ExceptionHandler(UserRegisterException.class)
    @ResponseBody
    public BaseRespVo userRegisterException() {
        return BaseRespVo.fail("用户注册异常");
    }

    /**
     * @author: lds
     * @scope: local(局部)
     */
    @ExceptionHandler(UserUpdateException.class)
    @ResponseBody
    public BaseRespVo userUpdateException() {
        return BaseRespVo.fail("用户修改异常");
    }

    /**
     * @author: lds
     * @scope: local(局部)
     */
    @ExceptionHandler(UserLoginException.class)
    @ResponseBody
    public WxBaseRespVo userLoginException(UserLoginException userLoginException) {
        return WxBaseRespVo.fail2(userLoginException.getMessage());
    }

    /**
     * @author: lsy
     * @scope: local(局部)
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public BaseRespVo httpMessageNotReadableException(HttpMessageNotReadableException exception) {
        return BaseRespVo.fail("传入参数类型错误");
    }

    /**
     * @author: lsy
     * @scope: global(全局)
     */
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public BaseRespVo bindException(BindException exception) {
        return BaseRespVo.fail("参数类型错误");
    }

    /**
     * yfs
     * 局部
     * @return
     */
   @ExceptionHandler(CartException.class)
    @ResponseBody
    public BaseRespVo cartException(CartException carException){
        return BaseRespVo.fail(carException.getMsg());
   }

   @ExceptionHandler(MailSendException.class)
    @ResponseBody
    public BaseRespVo mailSendException(MailSendException exception) {
       return BaseRespVo.fail("请输入正确的邮箱号");
   }
}
